import { Controller, Get, Post, Body, Param, Query, Res, Req, Session, Patch,Delete } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import * as svgCaptcha from 'svg-captcha'
import session from 'express-session';

//import  * as uuid from 'uuid'
//import { query } from 'express';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

//  POST 使用@Body()  获取数据
//  GET  使用@Query() 获取数据
//  获取参数 使用@Param('id')

  @Post()
  create(@Body() CreateUserDto: CreateUserDto){
   return this.userService.create(CreateUserDto)
  }

 
  @Get() 
  findAll(@Query() query:{keyWord:string,page:number,pageSize:number}){
     return this.userService.findAll(query)
  }

  @Patch(':id')
  update(@Param('id') id:string, @Body() UpdateUserDto: UpdateUserDto){
    return this.userService.update(+id,UpdateUserDto)
  }

   
  @Delete(':id')
  removes(@Param('id') id:number){
     return this.userService.remove(+id)
  }


  @Get('code')
  createCode(@Req() req, @Res() res, @Session() session) {
    const Captcha = svgCaptcha.create({
      size: 4,
      fontSize: 50,
      width: 100,
      height: 34,
      background: '#242424'
    })

    session.code = Captcha.text;  //验证码 

   console.log("Code============>",session);
    
    res.type('image/svg+xml')
    res.send(Captcha.data)
  }
  
  
  @Get('defCode')
  get_defCode(@Session() session){
       return{
        code:200,
        defCode: session.code
       }
  }


  /**
   * Body是用户传来的数据，session是服务器保存的对象
   */
  @Post('create')
  createUser (@Body() body:{name:string,pwd:string,type:string}, @Session() session) {


    if (session.code.toLocaleLowerCase() === body.type.toLocaleLowerCase()) {
       return this.userService.login(body)
    }

    //验证码错误
    return {
      code: 200,
      message: "验证码错误",
      error: true
    }

  }

}



  
  // @Get(':id')  //管道-转换-判断 引入 ParseUUIDPipe
  // //getHello(@Param('id', ParseUUIDPipe) id: string) {
  //   getHello(@Param('id') id: number) {
  //   return uuid.v4();  //ebec287f-e4eb-43c0-b9cd-640e766fc333
  // }